package dao.impl;

import bean.Menu;
import bean.User;
import bean.VIP;
import dao.UserDao;
import org.omg.PortableInterceptor.INACTIVE;
import util.DBUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    @Override
    public User login(String username) {
        User user=new User();
        DBUtil.connect();
        ResultSet rs=null;
        try {
            rs=DBUtil.executeQuery("select * from admininfo where adminName=?",username);
            while (rs.next()){
                user.setAdminName(rs.getString("adminName"));
                user.setPassword(rs.getString("password"));
                user.setId(rs.getInt("id"));
                user.setEditDate(rs.getString("editDate"));
                user.setState(rs.getString("state"));
                user.setNote(rs.getString("note"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return user;
    }

    @Override
    public List<Menu> menu(Integer userId) {

        DBUtil.connect();
        ResultSet rs=null;
        List<Menu> menuinfo=new ArrayList<>();
        try {
            rs=DBUtil.executeQuery("select * from menuinfo where id in(select menuId from usermenu where userId=?)",userId);

            while(rs.next()){
                Menu menu=new Menu();
                menu.setId(rs.getInt("id"));
                menu.setMenuName(rs.getString("menuName"));
                menu.setTarget(rs.getString("target"));
                menu.setUrl(rs.getString("url"));
                menu.setParentId(rs.getInt("parentId"));
                menu.setIcon(rs.getString("icon"));
                menuinfo.add(menu);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return menuinfo ;
    }

    @Override
    public Integer add(User user) {
        DBUtil.connect();
        DBUtil.open();
        String sql="insert into admininfo(id,adminName,password,note) values(?,?,?,?)";
        try {
            return DBUtil.executeInsertOfGeneratedKey(sql,user.getId(),user.getAdminName(),user.getPassword(),user.getNote());
        } catch (SQLException e) {
            e.printStackTrace();
            DBUtil.rollback();
            return 0;
        }finally {
            DBUtil.commit();
            DBUtil.close();
        }
    }

    @Override
    public List<User> findAll() {
        DBUtil.connect();
        List<User> users=new ArrayList<>();
        ResultSet rs=null;
        try {
            rs=DBUtil.executeQuery("select * from admininfo");
            while (rs.next()){
                User user=new User();
                user.setId(rs.getInt("id"));
                user.setNote(rs.getString("note"));
                user.setPassword(rs.getString("password"));
                user.setAdminName(rs.getString("adminName"));
                user.setState(rs.getString("state"));
                user.setEditDate(rs.getString("editDate"));
                users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return users;
    }

    @Override
    public void update(User user) {
        DBUtil.connect();
        DBUtil.open();
        String sql="update admininfo set adminName=?,password=?,note=? where id=?";
        try {
            DBUtil.executeUpdate(sql,user.getAdminName(),user.getPassword(),user.getNote(),user.getId());
        } catch (SQLException e) {
            e.printStackTrace();
            DBUtil.rollback();
        }finally {
            DBUtil.commit();
            DBUtil.close();
        }
    }

    @Override
    public User findAlls(Integer userId) {
        User user=new User();
        DBUtil.connect();
        ResultSet rs=null;
        try {
            rs=DBUtil.executeQuery("select * from admininfo where id=?",userId);
            if (rs.next()){
                user.setId(rs.getInt("id"));
                user.setNote(rs.getString("note"));
                user.setPassword(rs.getString("password"));
                user.setAdminName(rs.getString("adminName"));
                user.setState(rs.getString("state"));
                user.setEditDate(rs.getString("editDate"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return user;
    }

    @Override
    public void delete(Integer userId) {
        DBUtil.connect();
        DBUtil.open();
        try {
            DBUtil.executeUpdate("delete from admininfo where id=?",userId);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.commit();
            DBUtil.close();
        }
    }

    @Override
    public List<VIP> vip_findAll() {
        DBUtil.connect();
        List<VIP> vips=new ArrayList<>();
        ResultSet rs=null;
        try {
            rs=DBUtil.executeQuery("select * from userinfo");
            while (rs.next()){
                VIP vip=new VIP();
                vip.setId(rs.getInt("id"));
                vip.setMemberNo(rs.getString("memberNo"));
                vip.setUserName(rs.getString("userName"));
                vip.setHendset(rs.getInt("hendset"));
                vip.setUrl(rs.getString("url"));
                vip.setEmail(rs.getString("email"));
                vips.add(vip);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return vips;
    }

    @Override
    public VIP vip_find(Integer vipId) {
        VIP vip=new VIP();
        DBUtil.connect();
        ResultSet rs=null;
        try {
            rs=DBUtil.executeQuery("select * from userinfo where id=?",vipId);
            if (rs.next()){
                vip.setId(rs.getInt("id"));
                vip.setMemberNo(rs.getString("memberNo"));
                vip.setUserName(rs.getString("userName"));
                vip.setHendset(rs.getInt("hendset"));
                vip.setUrl(rs.getString("url"));
                vip.setEmail(rs.getString("email"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return vip;
    }

    @Override
    public void vip_delete(Integer vipId) {
        DBUtil.connect();
        DBUtil.open();
        try {
            DBUtil.executeUpdate("delete from userinfo where id=?",vipId);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.commit();
            DBUtil.close();
        }
    }
}
